package com.fdb.efp.console.api.rest;

import java.util.List;
import java.util.Map;

import com.irdstudio.basic.framework.core.constant.PubConstant;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.irdstudio.efp.console.service.facade.SDeptService;
import com.irdstudio.efp.console.service.vo.SDeptVO;
import com.irdstudio.efp.e4a.service.facade.SRoleDataRuleService;
import com.irdstudio.basic.framework.core.constant.DataRuleType;
import com.irdstudio.basic.framework.core.constant.ResponseData;
import com.irdstudio.basic.framework.web.controller.AbstractController;

@RestController
@RequestMapping("/api")
public class SDeptController extends AbstractController  {
	
	@Autowired
	@Qualifier("sDeptService")
	private SDeptService sDeptService;
	@Autowired
	@Qualifier("sRoleDataRuleService")
	private SRoleDataRuleService sRoleDataRuleService;
	
	/**
	 * 列表数据查询
	 * @param page
	 * @param size
	 * @return
	 */
	@RequestMapping(value="/s/depts", method=RequestMethod.POST)
	public @ResponseBody ResponseData<List<SDeptVO>> querySDeptAll(
			@RequestBody SDeptVO vo) {
		// 根据用户角色获取用户权限方案
		List<SDeptVO> outputVo = null;
		Map<String,String> dataRuleTypeMap = sRoleDataRuleService.getDataRuleType(httpRequest.getRequestURI(), getUserRoles());
		vo.setPrdCodebyBaseInfo(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PRD_CODE));
		DataRuleType dataRuleType = DataRuleType.getDataRuleType(dataRuleTypeMap == null ? null:dataRuleTypeMap.get(PubConstant.PERMIS_TYPE));
		setUserInfoToVO(vo);
		// 根据权限方案调用不同的方案进行数据查询
		switch (dataRuleType) {
		case Owner:
			outputVo = sDeptService.queryAllOwner(vo);
			break;
		case CurrOrg:
			outputVo = sDeptService.queryAllCurrOrg(vo);
			break;
		case CurrDownOrg:
			outputVo = sDeptService.queryAllCurrDownOrg(vo);
			break;
		default:
			outputVo = sDeptService.queryAllOwner(vo);
			break;
		}
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键查询详情
	 * @return
	 */
	@RequestMapping(value="/s/dept/{depno}", method=RequestMethod.GET)
	public @ResponseBody ResponseData<SDeptVO> queryByPk(@PathVariable("depno") String depno) {		
		SDeptVO inVo = new SDeptVO();
				inVo.setDepno(depno);
		SDeptVO outputVo = sDeptService.queryByPk(inVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键删除信息
	 * @param sDept
	 * @return
	 */
	@RequestMapping(value="/s/dept", method=RequestMethod.DELETE)
	public @ResponseBody ResponseData<Integer> deleteByPk(@RequestBody SDeptVO inSDeptVo) {		
		int outputVo = sDeptService.deleteByPk(inSDeptVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 根据主键更新信息
	 * @param inSDeptVo
	 * @return
	 */
	@RequestMapping(value="/s/dept", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateByPk(@RequestBody SDeptVO inSDeptVo) {		
		int outputVo = sDeptService.updateByPk(inSDeptVo);
		return getResponseData(outputVo);
		
	}
	
	/**
	 * 新增数据
	 * @param inSDeptVo
	 * @return
	 */
	@RequestMapping(value="/s/dept", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertSDept(@RequestBody SDeptVO inSDeptVo) {
		int outputVo = sDeptService.insertSDept(inSDeptVo);
		return getResponseData(outputVo);
		
	}
	
	
	/**
	 * 更新待生效状态部门信息
	 * @param inSDeptVo
	 * @return
	 */
	@RequestMapping(value="/s/dept/submit", method=RequestMethod.PUT)
	public @ResponseBody ResponseData<Integer> updateDeptTemp(@RequestBody SDeptVO inSDeptVo) {
		int outputVo = sDeptService.updateDeptTemp(inSDeptVo);
		return getResponseData(outputVo);
		
	}
	
	
	/**
	 * 新增状态生效的部门数据
	 * @param inSDeptVo
	 * @return
	 */
	@RequestMapping(value="/s/dept/submit", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> insertDeptTemp(@RequestBody SDeptVO inSDeptVo) {
		int outputVo = sDeptService.insertDeptTemp(inSDeptVo);
		return getResponseData(outputVo);
		
	}
	
	
	/**
	 * 让部门状态失效
	 * @param inSDeptVo
	 * @return
	 */
	@RequestMapping(value="/s/dept/disable", method=RequestMethod.POST)
	public @ResponseBody ResponseData<Integer> disableDeptTemp(@RequestBody SDeptVO inSDeptVo) {
		int outputVo = sDeptService.disableDeptTemp(inSDeptVo);
		return getResponseData(outputVo);
		
	}
	
	
	
}